package com.buka.user.controller;

import com.buka.user.entity.User;
import com.buka.user.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 接收点击按钮的请求  渲染用户信息页面
 */
@WebServlet("/toUpdateUser")
public class ToUpdateUserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 加载用户信息
        String id = req.getParameter("id");
        //查询数据库
        UserService userService = new UserService();
        User user = userService.selectById(id);
        //id 是唯一的，通过id 去查只会有两种结果
        //1.返回一条记录 对应一个对象
        //2.查不到  空
        if(null == user) {
            //跳转错误页面
            req.setAttribute("errMsg","用户不存在");
            //重定向获取不到错误信息的
//            resp.sendRedirect("/error.jsp");
            req.getRequestDispatcher("/error.jsp").forward(req, resp);
        }else{
            //传递user对象
            req.setAttribute("user",user);
            //在jsp中回显用户数据
            req.getRequestDispatcher("/WEB-INF/updateUser.jsp").forward(req, resp);
        }


    }
}
